/*
Copyright (C) 1996-1997 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
// gl_vidnt.c -- NT GL vid component

#include "quakedef.h"
#include <dwmapi.h>

void VID_SetWindowStyles (HWND hWndStyle, BOOL windowed)
{
	// set the styles as appropriate
	if (windowed)
		SetWindowLong (hWndStyle, GWL_STYLE, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_VISIBLE);
	else SetWindowLong (hWndStyle, GWL_STYLE, WS_POPUP);

	// and update the styles
	SetWindowPos (hWndStyle, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED);
	Sleep (10);
	Sys_SendKeyEvents ();
}


void VID_MoveWindow (HWND hWndMove, int width, int height)
{
	RECT workarea;

	SystemParametersInfo (SPI_GETWORKAREA, 0, &workarea, 0);

	MoveWindow (
		hWndMove,
		workarea.left + (RectWidth (&workarea) - width) / 2,
		workarea.top + (RectHeight (&workarea) - height) / 2,
		width,
		height,
		TRUE
	);

	Sleep (10);
	Sys_SendKeyEvents ();
}


void VID_CenterWindow (HWND hWndCenter)
{
	RECT windowrect;

	GetWindowRect (hWndCenter, &windowrect);
	VID_MoveWindow (hWndCenter, RectWidth (&windowrect), RectHeight (&windowrect));
}


BOOL VID_CreateWindow (D3DDISPLAYMODE *mode, BOOL windowed)
{
	WNDCLASS wc;

	// Register the frame class
	wc.style         = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc   = (WNDPROC) MainWndProc;
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = GetModuleHandle (NULL);
	wc.hIcon         = LoadIcon (GetModuleHandle (NULL), MAKEINTRESOURCE (IDI_ICON2));
	wc.hCursor       = NULL;
	wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
	wc.lpszMenuName  = 0;
	wc.lpszClassName = "DirectQRedux";

	if (!RegisterClass (&wc))
	{
		Sys_Error ("Couldn't register window class");
		return FALSE;
	}

	// initially create the window unstyled and unsized; we'll set it up properly when done
	if ((vid.Window = CreateWindow ("DirectQRedux", "DirectQRedux", 0, 1, 1, 1, 1, NULL, NULL, GetModuleHandle (NULL), NULL)) == NULL)
	{
		Sys_Error ("Couldn't create DIB window");
		return FALSE;
	}

	// update the mode for vid_width and vid_height
	mode = VID_VerifyMode (mode, windowed);

	VID_ResizeWindow (vid.Window, mode, windowed);

	vid.Width = mode->Width;
	vid.Height = mode->Height;
	vid.RecalcRefdef = TRUE;

	Con_Printf (PRINT_SAFE, "Initialized video mode: ");//\n", mode->Width, mode->Height);
	VID_DescribeCurrentMode_f ();

	return TRUE;
}


void VID_ResizeWindow (HWND hWndResize, D3DDISPLAYMODE *mode, BOOL windowed)
{
	RECT windowrect;

	// suppress WM_SIZE messages during the window resize operation
	vid.SuppressWMSize = TRUE;

	// update the styles
	VID_SetWindowStyles (hWndResize, windowed);

	// update the rect
	SetRect (&windowrect, 0, 0, mode->Width, mode->Height);
	AdjustWindowRect (&windowrect, GetWindowLong (hWndResize, GWL_STYLE), FALSE);

	// and move/resize it as required
	VID_MoveWindow (hWndResize, RectWidth (&windowrect), RectHeight (&windowrect));

	// start listening to them again
	vid.SuppressWMSize = FALSE;
}


// DWM crap - must load dynamically so that we can run on XP
/*
typedef HRESULT (WINAPI *pfnDwmIsCompositionEnabled) (BOOL *pfEnabled);
typedef HRESULT (WINAPI *pfnDwmSetWindowAttribute) (HWND hwnd, DWORD dwAttribute, LPCVOID pvAttribute, DWORD cbAttribute);
typedef HRESULT (WINAPI *pfnDwmGetWindowAttribute) (HWND hwnd, DWORD dwAttribute, LPCVOID pvAttribute, DWORD cbAttribute);

pfnDwmIsCompositionEnabled s_DwmIsCompositionEnabled;
pfnDwmSetWindowAttribute s_DwmSetWindowAttribute;
pfnDwmGetWindowAttribute s_DwmGetWindowAttribute;
*/

void VID_ToggleDWM (void)
{
	/*
	HINSTANCE dwmInst = LoadLibrary ("dwmapi.dll");

	if (dwmInst)
	{
		s_DwmIsCompositionEnabled = (pfnDwmIsCompositionEnabled) GetProcAddress (dwmInst, "DwmIsCompositionEnabled");
		s_DwmSetWindowAttribute = (pfnDwmSetWindowAttribute) GetProcAddress (dwmInst, "DwmSetWindowAttribute");
		s_DwmGetWindowAttribute =  (pfnDwmGetWindowAttribute) GetProcAddress (dwmInst, "DwmGetWindowAttribute");

		if (s_DwmIsCompositionEnabled && s_DwmSetWindowAttribute && s_DwmGetWindowAttribute)
		{
			DWMNCRENDERINGPOLICY ncrp = DWMNCRP_DISABLED;

			// Disable non-client area rendering on the window.
			s_DwmSetWindowAttribute (vid.Window, DWMWA_NCRENDERING_POLICY, &ncrp, sizeof (ncrp));
		}
		else Con_Printf (PRINT_SAFE, "DWM is not available on this machine\n");

		FreeLibrary (dwmInst);
	}
	else Con_Printf (PRINT_SAFE, "DWM is not available on this machine\n");
	*/
}


